package com.cyz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cyz.dao.ProductDao;
import com.cyz.dao.ShopcartDao;
import com.cyz.pojo.Product;
import com.cyz.pojo.Shopcart;
import com.cyz.util.DBOperation;
import com.cyz.util.Dbutil;
import com.cyz.util.MD5Util;

public class ShopcartDaoImpl  implements ShopcartDao{
	static Connection Conn=Dbutil.getConnection();
	static PreparedStatement pst;
	/**
	 * ͨ���û�ID����ȡ���Ĺ��ﳵ��Ϣ
	 * @param UseId
	 * @return
	 * @throws SQLException
	 */
	@Override
	public List<Shopcart> getShopcartByUseId(Integer useId) throws SQLException  {
		
			ProductDao productDao=new ProductDaoImpl();
			Product product=new Product();
			List<Shopcart> shopcartList=new ArrayList<Shopcart>();
			ResultSet rs = null;
			String sql=	"select * from shopcart where userId=?";
			List<Object> param = new ArrayList<Object>();
			param.add(useId);
			rs = DBOperation.query(sql, param); 
			while(rs.next())
			{
				Shopcart shopcart=new Shopcart();
				shopcart.setId(rs.getInt("id"));
				shopcart.setUserId(rs.getInt("userId"));
				Integer productId=rs.getInt("productId");
				product=productDao.getProductById(productId);
				shopcart.setProduct(product);
				shopcart.setPrice(rs.getFloat("price"));
				shopcart.setAmount(rs.getInt("amount"));
				shopcart.setProductId(productId);
				shopcartList.add(shopcart);
			}
		return shopcartList;
	
	}
/**
 * 
 *获取当前商品数据将该条记录 加入购物车
 * @throws SQLException 
 */
	@Override
	public void insertShopcart(Integer userId, Integer productId, Integer amount, float price) throws SQLException {
		// TODO Auto-generated method stub
		  ResultSet rs = null;
		   pst=Conn.prepareStatement("insert into shopcart(userId,productId,amount,price) values(?,?,?,?)");
		   pst.setInt(1, userId);
		   pst.setInt(2, productId);
		   pst.setInt(3, amount);
		   pst.setFloat(4, price);
		   pst.execute();
	}
	/////////////////
	/**
	  *  ɾ�����ﳵ��¼
	 * @param userName �û���
	 * @param productName ��Ʒ�� 
	 */
	@Override
	public void deteleShopcart(String userName, String productName) {
		List<Object> param = new ArrayList<Object>();
		String sql = "delete from shopcart where userId=(select id from  user where userName=?) and productId=(select id from  product where productName=?)";
		param.add(userName);
		param.add(productName);
		
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 查询是否存在该用户对该商品的购买
	 */
	@Override
	public Integer IsExist(Integer userId, Integer productId) {
		List<Object> param = new ArrayList<Object>();
		ResultSet rs=null;
		Integer temp=0;
		String sql = "select * from shopcart where userId=? and productId=?";
		param.add(userId);
		param.add(productId);
		rs=DBOperation.query(sql, param);
		try {
			while(rs.next())
			{
				temp=rs.getInt("amount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}
	/**
	 * 更新该条数据库记录
	 */
	public void updateShopcart(Integer userId,Integer productId,Integer amount)
	{
		List<Object> param = new ArrayList<Object>();
		
		String sql = "update  shopcart set amount=? where userId=? and productId=?";
		param.add(amount);
		param.add(userId);
		param.add(productId);
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 清空该用户购物车
	 * @param userName
	 * @param productName
	 */
	public void deteleAllShopcart(Integer userId)
	{
		List<Object> param = new ArrayList<Object>();
		String sql = "delete from shopcart where userId=?";
		param.add(userId);
		
		
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
	

